package org.flashbrighton.as3bots
{
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	import org.flashbrighton.as3bots.collision.Collidable;
	import org.flashbrighton.as3bots.collision.SweepTestMethods;
	import org.flashbrighton.as3bots.events.BotEvent;
	import org.flashbrighton.as3bots.geom.Metrics;
	import org.flashbrighton.as3bots.logic.*;

	
	public class Bot extends Collidable
	{
		
		[Embed(source="arial.ttf", fontFamily="Arial")]
		private var _arial:String;
		
		private var _logic:IBotLogic;
		internal var _playerName:String;		
		public var _colour:uint;		
		private var _maxSpeed:int;
		private var _maxRotation:int;
		private var _energy:int;
		private var _label:TextField;
		
		/**
		 * Constructor.
		 * 
		 * @param r The radius of the bot.
		 */
		public function Bot(m:Metrics, logic:IBotLogic, colour:uint)
		{
			super(m);
			
			_logic = logic;
			_colour = colour;
			
			metrics.sweepTestMethod = SweepTestMethods.circleToCircle;
			
			initValues();

			// Draw bot after being added to stage to get
			// accurate x and y positions.
			addEventListener(Event.ADDED_TO_STAGE, drawBot);
		}

		
		private function initValues():void
		{
			_maxSpeed = 10;
			_maxRotation = 25;
			_energy = 1000;
		}
		
		
		private function drawBot(event:Event=null):void
		{
			//_circle = new Circle(x, y, _radius);
			
			graphics.lineStyle(0, _colour);
			graphics.drawCircle(0, 0, metrics.circle.radius); 
			graphics.moveTo(0, 0);
			graphics.lineTo(metrics.circle.radius, 0);
			
			_label = new TextField();
			_label.embedFonts = true;
			_label.defaultTextFormat = new TextFormat("Arial", 12, 0xFFFFFF);
			_label.text = name;
			addChild(_label);
		}		
		
		public function get enginePercent():Number
		{
			return _logic.enginePercent;
		}
		
		public function get rotatorPercent():Number
		{
			return _logic.rotatorPercent;
		}
		
		public function get energy():int
		{
			return _energy;
		}
		
		public function set energy(val:int):void
		{
			_energy = val;
		}
		
		
		public function mainLoop():void
		{
			//energy -= 1
			_logic.run();
		}
		
		
		override public function notify(event:BotEvent):void
		{
			energy += event.points;
			_logic.notify(event);
		}
		
		
		public function move():void
		{
			var vel:int = Math.round(enginePercent * _maxSpeed);
			metrics.applyVelocity(vel);	
		}
		
		
		public function rotate():void
		{
			var rotateAmount:int = rotatorPercent * _maxRotation;	
			metrics.addRotation(rotateAmount)
			//_label.rotation = metrics.???
		}
		
		
		/**
		 * Finds the value based on where a bot gets hit (0 to 1).
		 */
		public function getHitPoint(enemy:Collidable):Number
		{
			var hitAngle:Number = Math.atan2(enemy.y - y, enemy.x - x) - (rotation * Math.PI/180);
			var hitPoint:Number = hitAngle/Math.PI;
			if (hitPoint > 1) hitPoint -= 2;
			if (hitPoint < -1) hitPoint += 2;
			return hitPoint;
		}

	}
}